package pri.damai.xiaowu.gateway.security.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import pri.damai.xiaowu.common.core.enums.ResultCommonEnum;
import pri.damai.xiaowu.gateway.enums.GatewayResultEnum;
import reactor.core.publisher.Mono;

/**
 * token 认证处理
 * @Desc
 * @Author DaMai
 * @Date 2021/3/23 15:26
 * 但行好事，莫问前程。
 */
@Component
@Primary
@Slf4j
public class XiaoWuAuthenticationManager implements ReactiveAuthenticationManager {


    @Override
    public Mono<Authentication> authenticate(Authentication authentication) {

        log.debug("路过 XiaoWuAuthenticationManager");

        // 已经通过验证，直接返回
        if (authentication.isAuthenticated()) {
            return Mono.just(authentication);
        }

        // 排除未认证异常
        throw new AuthenticationServiceException(GatewayResultEnum.UNAUTHORIZED.desc());
    }
}
